package com.tao.utilslib.data;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.InflaterOutputStream;

/**
 * 字符串压缩算法
 * 1. gzip
 * 2. flaterStream   耗时段压缩率高
 * <p>
 * 字符长度：78888890
 * 1. gzip
 * 压缩长度：28134468   压缩耗时：4663 ms
 * 解压长度：78888890   解压耗时：683 ms
 * 2.flater
 * 压缩长度：28134452   压缩耗时：3739 ms
 * 解压长度：78888890   解压耗时：837 ms
 */

public class StringZipUtils {

    /**
     * 使用gzip压缩字符串
     *
     * @param str 要压缩的字符串
     * @return
     */
    public static String compress(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new String(Base64Util.encode(out.toByteArray()));
    }

    public static byte[] compress(byte[] data) {
        if (data == null || data.length == 0) {
            return new byte[1];
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return out.toByteArray();
    }

    /**
     * 使用gzip解压缩
     *
     * @param compressedStr 压缩字符串
     * @return
     */
    public static String uncompress(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = Base64Util.decode(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }

    public static byte[] uncompress(byte[] data) {
        if (data == null || data.length == 0) {
            return new byte[0];
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        try {

            in = new ByteArrayInputStream(data);
            ginzip = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return out.toByteArray();
    }


    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < 1024*1024; i++) {
            sb.append("a" + i);
        }
        
        String str = sb.toString();
        long l = System.currentTimeMillis();

        System.err.println(" 字符长度：" + str.length()  +" 字符byte长度：" + str.getBytes().length + "   ");

        System.err.println("gzip");
        String compress = compress(str);
        System.err.println(" 压缩长度：" + compress.getBytes().length + "   压缩耗时：" + (System.currentTimeMillis() - l) + " ms ");

        l = System.currentTimeMillis();
        String uncompress = uncompress(compress);
        System.err.println(" 解压长度：" + uncompress.getBytes().length + "   解压耗时：" + (System.currentTimeMillis() - l) + " ms ");
 
        System.err.println("flater byte");
        l = System.currentTimeMillis();

        byte[] bytes = zipBaseByte(str.getBytes());
        System.err.println(" 压缩长度：" + bytes.length + "   压缩耗时：" + (System.currentTimeMillis() - l) + " ms ");
        l =System.currentTimeMillis();
        
        String s1 = new String(bytes);
        System.err.println(" " + s1.length());
        byte[] bytes1 = unzipByte(bytes);
        System.err.println(" 解压长度：" + bytes1.length + "   解压耗时：" + (System.currentTimeMillis() - l) + " ms ");
//        System.err.println(new String(bytes1));
        
        System.err.println("flater base64");
        l =System.currentTimeMillis();

        String zipBase64 = zipBase64(str);
        System.err.println(" 压缩长度：" + zipBase64.getBytes().length + "   压缩耗时：" + (System.currentTimeMillis() - l) + " ms ");
        l =System.currentTimeMillis();

        String unzipBase64 = unzipBase64(zipBase64);
        System.err.println(" 解压长度：" + unzipBase64.getBytes().length + "   解压耗时：" + (System.currentTimeMillis() - l) + " ms ");

//        
//        l = System.currentTimeMillis();
//
//        System.err.println(" GZIP byte");
//
//        System.err.println(" 字符byte长度：" + str.getBytes().length + "   ");
//
//        bytes = compress(str.getBytes());
//        System.err.println(" 压缩长度：" + bytes.length + "   压缩耗时：" + (System.currentTimeMillis() - l) + " ms ");
//        l = System.currentTimeMillis();
//
//        bytes1 = uncompress(bytes);
//        System.err.println(" 解压长度：" + bytes1.length + "   解压耗时：" + (System.currentTimeMillis() - l) + " ms ");
//        System.err.println(new String(bytes1));
    }

    /**
     * 压缩字符串,默认梳utf-8
     *
     * @param text
     * @return
     */
    public static String zipBase64(String text) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)) {
                deflaterOutputStream.write(text.getBytes(Charset.forName("utf-8")));
            }
            return new String(Base64Util.encode(out.toByteArray()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static byte[] zipBaseByte(byte[] data) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)) {
                deflaterOutputStream.write(data);
            }
            return out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    /**
     * 解压字符串,默认utf-8
     *
     * @param text
     * @return
     */
    public static String unzipBase64(String text) {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            try (OutputStream outputStream = new InflaterOutputStream(os)) {
                outputStream.write(Base64Util.decode(text));
            }
            return new String(os.toByteArray(), Charset.forName("utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static byte[] unzipByte(byte[] data) {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            try (OutputStream outputStream = new InflaterOutputStream(os)) {
                outputStream.write(data);
            }
            return os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }
}
